package com.test.blogsystem.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.test.blogsystem.Comment.Exception.BlogException;
import com.test.blogsystem.Mapper.BlogMapper;
import com.test.blogsystem.Pojo.DO.BlogInfo;
import com.test.blogsystem.Pojo.Require.UpdateBlogInfo;
import com.test.blogsystem.Pojo.Response.BlogAddResponse;
import com.test.blogsystem.Pojo.Response.BlogInfoResponse;
import com.test.blogsystem.Service.BlogService;
import com.test.blogsystem.Utils.MyBeanUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogMapper blogMapper;


    @Override
    public List<BlogInfoResponse> getList() {
        QueryWrapper<BlogInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BlogInfo::getDeleteFlag, 0);
        List<BlogInfo> blogInfos = blogMapper.selectList(queryWrapper);

        return blogInfos.stream().map(blogInfo -> MyBeanUtil.transBlogInfo(blogInfo))
                .collect(Collectors.toList());
    }

    @Override
    public BlogInfoResponse getBlogDetail(Integer blogId) {
        QueryWrapper<BlogInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BlogInfo::getId, blogId).eq(BlogInfo::getDeleteFlag, 0);
        BlogInfo blogInfo = blogMapper.selectOne(queryWrapper);

        return MyBeanUtil.transBlogInfo(blogInfo);
    }

    @Override
    public boolean addBlog(BlogAddResponse blogAddResponse) {
        BlogInfo blogInfo = new BlogInfo();
        BeanUtils.copyProperties(blogAddResponse, blogInfo);
        try {
            Integer ret = blogMapper.insert(blogInfo);
            if (ret == 1) return true;
            return false;
        } catch (Exception e) {
            log.error("博客插入失败, e:", e);
            throw new BlogException("内部错误, 请联系管理员");
        }
    }

    @Override
    public boolean updateBlogInfo(UpdateBlogInfo updateBlogInfo) {
        if (updateBlogInfo == null) return false;
        BlogInfo blogInfo = MyBeanUtil.transBlogInfo(updateBlogInfo);
        log.info("接收到的blogInfo为：{}",blogInfo);
        try {
            Integer num = blogMapper.updateById(blogInfo);
            if (num == 1) return true;
            return false;
        } catch (Exception e) {
            log.info("更新失败...");
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean deleteBlog(Integer blogId) {
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(1);
        try {
            Integer num = blogMapper.updateById(blogInfo);
            if (num == 1) return true;
            return false;
        } catch (Exception e) {
            log.info("更新失败...");
            throw new RuntimeException(e);
        }
    }
}
